package com.freemalll.merchant.controller;

import cn.hutool.core.util.RandomUtil;
import com.freemalll.common.core.annotation.ResultInterceptor;
import com.freemalll.common.core.exception.BusinessException;
import com.freemalll.merchant.common.CommonEnum;
import com.freemalll.merchant.common.CommonEnumDto;
import com.freemalll.merchant.domain.vo.AreaAndProvinceVo;
import com.freemalll.merchant.mapper.common.CommonMapper;
import com.freemalll.merchant.utils.AliOSSUtil;
import com.freemalll.merchant.utils.RedisUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Tag(name = "公共接口：公共接口管理")
@RestController
@RequestMapping("/common")
@RequiredArgsConstructor
@ResultInterceptor
@Validated
public class CommonController {

    @Resource
    private CommonMapper commonMapper;
    @Resource
    private AliOSSUtil aliOSSUtil;
    @Resource
    private RedisUtil redisUtil;

    @Operation(summary = "获取枚举列表")
    @GetMapping("/enumList")
    public List<CommonEnumDto> enumList() {
        return CommonEnum.getEnumsList();
    }

    @GetMapping("/getLongLink")
    @Operation(summary = "通过短连接获取长链接")
    public String getCityAndRegion(@RequestParam(value = "imagePath") String imagePath) {
        return aliOSSUtil.getFileUrl(imagePath);
    }

    @GetMapping("/getProvince")
    @Operation(summary = "获取省份")
    public List<Map<String, String>> getProvince() {
        return commonMapper.getProvince();
    }

    @GetMapping("/getCity")
    @Operation(summary = "根据省份获取城市")
    public List<Map<String, String>> getCity(@RequestParam(value = "province") String province) {
        return commonMapper.getCity(province);
    }

    @GetMapping("/getRegion")
    @Operation(summary = "根据城市获取区域")
    public List<Map<String, String>> getRegion(@RequestParam(value = "city") String city) {
        return commonMapper.getRegion(city);
    }


    @GetMapping("/getAreaAndProvince")
    @Operation(summary = "获取销售区域和省份")
    public List<AreaAndProvinceVo> getAreaAndProvince() {
        List<AreaAndProvinceVo> areaAndProvince = commonMapper.getAreaAndProvince();
        //递归获取
        if (CollectionUtils.isNotEmpty(areaAndProvince)) {
            areaAndProvince = buildTree(areaAndProvince, null);
        }
        return areaAndProvince;
    }
    private List<AreaAndProvinceVo> buildTree(List<AreaAndProvinceVo> paramsDetailList, String parentId) {
        return paramsDetailList.stream()
                .filter(e -> Objects.equals(e.getParentId(), parentId))
                .map(e -> {
                    // 修改对象属性并返回自身，用于构建树结构
                    e.setChildren(buildTree(paramsDetailList, e.getParamKey()));
                    return e;
                })
                .collect(Collectors.toList());
    }

    @Operation(summary = "文件上传")
    @PostMapping("/uploadFile")
    public Map<String, String> uploadFile(@RequestParam("file") MultipartFile file) {
        String fileUrl;
        Map<String, String> map = new HashMap<>();
        if (file == null) {
            throw BusinessException.businessParamsNotExist("文件不能为空");
        }
        try {
            InputStream inputStream = file.getInputStream();
            String key = aliOSSUtil.uploadFile(inputStream, "ufq/file", System.currentTimeMillis() + "-" + RandomUtil.randomString(10) + "-" + file.getOriginalFilename());
            fileUrl = aliOSSUtil.getFileUrl(key);
            map.put("shortPath", key);
            map.put("path", fileUrl);
        } catch (Exception e) {
            throw BusinessException.businessValidateError("文件上传失败：" + e.getMessage());
        }
        return map;
    }

}
